package org.za.communication.message;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;

public class RegisterMessage extends BaseMessage {

	private byte[] key = new byte[4];
	private String phone;
	private String name;
	private String password;
	private String code;

	public byte[] getKey() {
		return key;
	}

	public void setKey(byte[] key) {
		this.key = key;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public RegisterMessage() {
		super();
		this.msgId = 0x01;
	}

	@Override
	public IoBuffer getData() {
		try {
			buf = IoBuffer.allocate(2).setAutoExpand(true);
			buf.putUnsignedShort(this.getMsgId());
			buf.put(key);
			buf.putString(phone, Charset.forName("utf-8").newEncoder());
			buf.putString(name, Charset.forName("utf-8").newEncoder());
			buf.put(new byte[1]);
			buf.putString(password, Charset.forName("utf-8").newEncoder());
			buf.putString(code, Charset.forName("utf-8").newEncoder());
			buf.flip();
			return buf;
		} catch (Exception ee) {
			return null;
		}

	}

	@Override
	public void setData(IoBuffer buf) {
		try {
			this.buf = buf;
			buf.get(key);
			phone = buf.getString(11, Charset.forName("utf-8").newDecoder());
			int index = buf.indexOf((byte) 0) - buf.position();
			if (index == 0) {
				buf.get();
			} else if (index > 0) {
				name = buf.getString(index, Charset.forName("utf-8")
						.newDecoder());
				buf.get();
			}
			password = buf.getString(8, Charset.forName("utf-8").newDecoder());
			code = buf.getString(6, Charset.forName("utf-8").newDecoder());

		} catch (Exception ee) {

		}
	}

}
